package www.houdoudou.com.supplier;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;

public  class IndexManagement {
	
	public static	JSONObject  managerIndex(String jsonData){
		JSONObject result = new JSONObject();
		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
		//判断请求的功能
		int FunctionCode=jsonfrom.getInt("FunctionCode");
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction.getCurrentConnection();
		   // 此值代表用户身份，不同的身份，对于同一个增删改查函数，有不同的处理，防止出错。
		   int Manager_Authority= jsonfrom.getInt("Manager_Authority");
		   switch(FunctionCode){
		   
		// 查询 
			case 1:{
				
					
					// step2 查询字管理员
					  
				 int Index_ID=jsonfrom.getInt("Index_ID");
				   try {
					   String sql = "select * from Tb_Index where Index_ID="+Index_ID+"";    
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
					if(rs.next()){	
						            boolean isAuthorityEnough=false;
						            if(Manager_Authority<4) isAuthorityEnough=true;
                                    if(Manager_Authority==4) {
                                    	if(jsonfrom.getString("Owner_Telnum").equals(rs.getString("Owner_Telnum"))){
                                    		isAuthorityEnough=true;
                                    	}else{
                                    		result.put("ActionStatus", false);
        									result.put("ErrorInfo", "无权查询该目录");
                                    	}
                                    }
                                    if(Manager_Authority==5) {
                                    	if(jsonfrom.getString("Supplier_Telnum").equals(rs.getString("Supplier_Telnum"))){
                                    		isAuthorityEnough=true;
                                    	}else{
                                    		result.put("ActionStatus", false);
        									result.put("ErrorInfo", "无权查询该目录");
                                    	}
                                    }
                                    if(Manager_Authority==6) {
                                    	isAuthorityEnough=true;
                                    }
                                   
                                    if(isAuthorityEnough){
									result.put("ActionStatus", true);
									result.put("ErrorInfo", "查询成功");						
									result.put("Index_Name", rs.getString("Index_Name"));
									result.put("Index_Time_Flag", rs.getString("Index_Time_Flag"));
									result.put("Index_Temp_Flag", rs.getString("Index_Temp_Flag"));
									result.put("Index_Show_No", rs.getInt("Index_Show_No"));
									result.put("Index_Update_No", rs.getInt("Index_Update_No"));
                                    }
								
									
								
							}else{
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "要查询的目录不存在");
							}
							
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "数据库查询异常");
						}    			
					   break;     } //case 1
			//修改 按身份进行了区别。
			case 2:{
				 int Index_ID=jsonfrom.getInt("Index_ID");
				   try {
					   String sql = "select * from Tb_Index where Index_ID="+Index_ID+"";    
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
						rs= stm.executeQuery(sql);
					if(rs.next()){		
							 Manager_Authority=jsonfrom.getInt("Manager_Authority");
							switch(Manager_Authority){
						
							case 5:{
								rs.updateString("Index_Name", jsonfrom.getString("Index_Name"));
								rs.updateString("Index_Time_Flag", jsonfrom.getString("Index_Time_Flag"));
								rs.updateString("Index_Temp_Flag", jsonfrom.getString("Index_Temp_Flag"));
								
								rs.updateInt("Index_Show_No", jsonfrom.getInt("Index_Show_No"));
								rs.updateInt("Index_Update_No", rs.getInt("Index_Update_No")+1);
								rs.updateRow();
								result.put("ActionStatus", true);
								result.put("ErrorInfo", "更新成功");
							
							                break;}
							
						  default:{
							  result.put("ActionStatus", false);
								result.put("ErrorInfo", "无权修改");
						           break;    }
							
							
							}																																						
					
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "要修改的目录不存在");
						}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "数据库查询异常");
				}
				
			break;} //case 2
			
			case 3:{
			     // 只允许商家删 这里还必须做一个判断，
							if(Manager_Authority==5){
								// 这里还必须做一个判断 判断是否有产品
								 String Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");
								 JSONArray Idexes= jsonfrom.getJSONArray("Indexes");
								 for(int i=0;i<Idexes.size();i++){
								 try{						
									
									 int  Index_ID=Idexes.getJSONObject(i).getInt("Index_ID");
							
								  
									 String	sql	=" delete from  Tb_Index where Index_ID="+Index_ID+" and Supplier_Telnum="+Supplier_Telnum+"";									   
									             stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);  							           
									            		 stm.executeUpdate(sql);  	
									     				
															result.put("ActionStatus", true);
															result.put("ErrorInfo", "删除成功");
												} catch (SQLException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
													result.put("ActionStatus", false);
													result.put("ErrorInfo", "数据库删除操作失败");
												    break;
												} 							
										
								 }// end of for	
									
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "无权操作");}
						
				
				
			break;}
			
			// 新增 
			case 4:{
				 if(Manager_Authority==5){
				
					 try{
										
						  String sql = "insert  into Tb_Index (Index_Name,Supplier_Telnum,Index_Time_Flag,Index_Temp_Flag,Index_Update_No,Index_Show_No,Owner_Telnum) values (?,?,?,?,?,?,?)";
			              	pstm=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
							pstm.setString(1, jsonfrom.getString("Index_Name"));
							pstm.setString(2, jsonfrom.getString("Supplier_Telnum"));
							pstm.setString(3, jsonfrom.getString("Index_Time_Flag"));
							pstm.setString(4, jsonfrom.getString("Index_Temp_Flag"));
							pstm.setInt(5, 1);
							pstm.setInt(6, jsonfrom.getInt("Index_Show_No"));
							pstm.setString(7, jsonfrom.getString("Owner_Telnum"));
							
						    pstm.executeUpdate();
						    result.put("ActionStatus", true);
							result.put("ErrorInfo", "增加成功");
							rs=pstm.getGeneratedKeys();
							if(rs.next())
							result.put("Index_ID", rs.getInt(1));
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "数据库异常");
						}
						 }// end of if manager_authority==3
						 else{
							 result.put("ActionStatus", false);
								result.put("ErrorInfo", "无权操作");}			
			   break;}// case 4
			//查询所有 必须带着供应商的参数来查，不然没有意义
			 case 5:{
				  try {
					  String Supplier_Telnum=jsonfrom.getString("Supplier_Telnum");	 
					  String sql = null;
					  // 要添加 按时间标签，温度标签来查询下，但是为了不影响原来的使用，不能随便加	Query_Way,这里用Manager_Authority来判断，是顾客才要求获取 Query_Way.
					  //暂时不想改 供应商查询目录的android代码，这里写得有点乱，
					  // 以后这两个 if(Manager_Authority==)要用 swich Query_Way代替
					  if(Manager_Authority==6){
						  int Query_Way=jsonfrom.getInt("Query_Way");
						switch(Query_Way){
						case 2:{   String Index_Time_Flag=jsonfrom.getString("Index_Time_Flag");
						           String Index_Temp_Flag=jsonfrom.getString("Index_Temp_Flag");
						           sql = "select * from  Tb_Index where Supplier_Telnum="+Supplier_Telnum+" and Index_Time_Flag="+Index_Time_Flag+" and Index_Temp_Flag="+Index_Temp_Flag+""; 
						                   break;}
						
						}  
					  }
					 
					  // 以下这一条，要用 Query_Way=1来代替
					  if(Manager_Authority==5){
						  sql = "select * from  Tb_Index where Supplier_Telnum="+Supplier_Telnum+"";  }
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
						JSONArray Indexes= new JSONArray();
						while(rs.next()){
							JSONObject index= new JSONObject();
							boolean isadd=false;
						switch(Manager_Authority){							            
						case 5:{					
								isadd=true;
							        break;}	
						case 6:{					
							isadd=true;
						        break;}	
			              }
						if(isadd){
							index.put("Index_ID", rs.getInt("Index_ID"));
							index.put("Index_Name", rs.getString("Index_Name"));
							index.put("Index_Time_Flag", rs.getString("Index_Time_Flag"));
							index.put("Index_Temp_Flag", rs.getString("Index_Temp_Flag"));
							index.put("Index_Show_No", rs.getInt("Index_Show_No"));
							index.put("Index_Update_No", rs.getInt("Index_Update_No"));
	                        Indexes.add(index);               
						                         }
						
						}
						result.put("ActionStatus", true);
						result.put("ErrorInfo", "成功");
						result.put("Indexes",Indexes);
				   
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "查询失败");
					} 
						
						
					 break;  } //case5
			
			
			
		   }// end switch functioncode
		   
		  CommonFunction.closeConn(conn, pstm, stm, rs);
		   
		   return result;
	}
	
	
	
	
	

}
